// pages/component/realtime/realtime.js
const app = getApp();
const api = require('../../../apis/request.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    gSerial:{
      type:String,
      value:''
      // ,
      // observer: function (newVal, oldVal){
      //   console.log("gSerial =  " + this.newVal );
      // }
    },
    addr: {
      type: String,
      value: ''
      // ,
      // observer: function (newVal, oldVal) {
      //   console.log("addr = " + this.addr);
      // }
    },
    name: {
      type: String,
      value: ''
      // ,
      // observer: function (newVal, oldVal) {
      //   console.log("addr = " + this.addr);
      // }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    createItem: function (datas, label) {
      var _this = this;
      var dataKey = label.elementSort;
      var dataValue = datas[0][dataKey];
      var dataUnit = label.elementUnit;
      if (dataValue == undefined) {
        dataValue = "0";
      }
      var item = { "url": "", "name": label.elementName, "value": dataValue + " " + dataUnit };

      // console.log("dataKey = " + dataKey);

      // console.log("dataValue = "+datas[0][dataKey]);
      switch (label.elementId) {
        case 1:
        case 39:
          //空气温度
          //超温度
          //组件温度1
          //组件温度2
          item.url = "../../../img/elements/icon0_wendu.png";
          break;
        case 2:
          //相对湿度
          item.url = "../../../img/elements/icon0_shidu.png";
          break;
        case 4:
        case 51:
          //底层风速
          //超风速
          item.url = "../../../img/elements/icon0_fengsu.png";
          break;
        case 5:
        case 52:
          //底层风向
          //超风向
          item.url = "../../../img/elements/icon0_fengxiang.png";
          break;
        case 8:
          //土壤温度1
          item.url = "../../../img/elements/icon0_wendu_turang.png";
          break;
        case 9:
          //土壤湿度1
          item.url = "../../../img/elements/icon0_shidu_turang.png";
          break;
        case 6:
        case 41:
        case 42:
        case 43:
          //降雨量
          //二十四小时雨量
          //连续雨量
          item.url = "../../../img/elements/icon0_yuliang.png";
          break;
        case 12:
          //光照度
          item.url = "../../../img/elements/icon0_guanzhaoqiangdu.png";
          break;
        case 30:
          //二氧化碳
          item.url = "../../../img/elements/icon0_eryanghantan.png";
          item.name = "CO₂浓度";
          break;
        case 28:
          //PM2.5
          item.url = "../../../img/elements/icon0_pm2p5.png";
          item.name = "PM₂.₅";
          break;

        case 29:
          //PM₁₀
          item.url = "../../../img/elements/icon0_pm10.png";
          item.name = "PM₁₀";
          break;
        case 3:
          //大气压力
          item.url = "../../../img/elements/icon0_daqiyali.png";
          break;
        case 7:
          //蒸发量
          item.url = "../../../img/elements/icon0_zhengfaliang.png";
          break;
        case 40:
          //露点温度
          item.url = "../../../img/elements/icon0_ludianwendu.png";
          break;
        case 14:
        case 16:
        case 18:
        case 20:
        case 22:
          //总辐射，斜面辐射，散射辐射，光合有效辐射
          item.url = "../../../img/elements/icon0_fusheqiangdu.png";
          break;
        case 15:
        case 17:
        case 19:
        case 21:
        case 23:
          //总辐射累积量，斜面辐射累积量，散射辐射累积量
          item.url = "../../../img/elements/icon0_fusheleiji.png";
          break;
        case 26:
          //日照时数
          item.url = "../../../img/elements/icon0_rizhaoshichang.png";
          break;
        case 10:
        case 46:
          //土壤电导率
          item.url = "../../../img/elements/icon0_turangdaodianlv.png";
          break;
        case 11:
        case 45:
          //土壤PH
          item.url = "../../../img/elements/icon0_turangph.png";
          break;
        case 37:
        case 44:
          //叶面温度
          item.url = "../../../img/elements/icon0_yemianwendu.png";
          break;
        case 38:
          //叶面湿度
          item.url = "../../../img/elements/icon0_yemianshidu.png";
          break;
        case 27:
          //噪声
          item.url = "../../../img/elements/icon0_zaosheng.png";
          break;
        case 31:
          //SO₂
          item.url = "../../../img/elements/icon0_so2.png";
          item.name = "SO₂";
          break;
        case 35:
          //NO₂
          item.url = "../../../img/elements/icon0_no2.png";
          item.name = "NO₂";
          break;
        case 36:
          //CO
          item.url = "../../../img/elements/icon0_co.png";
          item.name = "CO";
          break;

        case 34:
          //H₂S
          item.url = "../../../img/elements/icon0_h2s.png";
          item.name = "H₂S";
          break;
        case 33:
          //O₃
          item.url = "../../../img/elements/icon0_o3.png";
          item.name = "O₃";
          break;
        case 49:
          //NH₃
          item.url = "../../../img/elements/icon0_nh3.png";
          item.name = "NH₃";
          break;
        case 47:
          //溶解氧
          item.url = "../../../img/elements/icon0_rongjieyang.png";
          break;
        case 48:
          //浊度
          item.url = "../../../img/elements/icon0_zhuodu.png";
          break;
      }

      return item;
    }
  },
  /**
   * component生命周期
   */
  lifetimes:{
    // created: function () { console.log("created"); }, // 组件在内存中创建完毕执行
   
    // detached: function () { console.log("detached"); }, // 组件移除执行
    // moved: function () { console.log("moved"); }, // 组件移动的时候执行

    attached: function () {
      // 在组件实例进入页面节点树时执行
      // var _this=this;
      // console.log("gSerial =  " + _this.data.gSerial + "\n addr = " + _this.data.addr);
      // console.log("attached") ;
    },
    ready: function () {
      // console.log("ready"); 
      var _this = this;
      // console.log("gSerial =  " + _this.data.gSerial + "\n addr = " + _this.data.addr);
      
      api.requestRealtime(_this.data.gSerial, _this.data.addr, function (res) {
        var data = res.data;
        // console.log(JSON.stringify(data));
        // _this.setData({url:data.datas[0].playUrl});
        // var cameraList = data.datas;
        // _this.setData({ cameraList: cameraList });
        if (data.status != 0) {
          wx.showToast({
            title: '' + res.msg,
            icon: 'failed',
            duration: 2000,
            mask: true,

          });
          return;
        }
        //请求成功
        var labels = data.labels;
        var datas = data.datas;
        var itemList = new Array();
        for (var index in labels) {
          // 测试添加数据
          // itemList.push(_this.createItem(datas, labels[index]))
          // itemList.push(_this.createItem(datas, labels[index]))
          // itemList.push(_this.createItem(datas, labels[index]))
          // itemList.push(_this.createItem(datas, labels[index]))
          itemList.push(_this.createItem(datas, labels[index]))
        }
        // console.log("stationName = " + _this.data.name + "\nupdateTime = " + datas[0].update_time)
        _this.setData({ stationName: _this.data.name, updateTime: datas[0].update_time, listdata: itemList });
      });


      // wx.request({
      //   url: app.globalData.URL + "/data/realtime",
      //   header: {
      //     // "Accept": "application/json",
      //     "Content-Type": "application/x-www-form-urlencoded",
      //     "token": app.globalData.token
      //   },
      //   method: "POST",
      //   data: { gSerial: _this.data.gSerial, collectorAddr: _this.data.addr},
      //   // data: { username: this.data.username, password: md5.hexMD5(this.data.password) },
      //   complete: function (res) {
      //     var data = res.data;
      //     // console.log(JSON.stringify(data));
      //     // _this.setData({url:data.datas[0].playUrl});
      //     // var cameraList = data.datas;
      //     // _this.setData({ cameraList: cameraList });
      //     if (data.status != 0) {
      //       wx.showToast({
      //         title: '' + res.msg,
      //         icon: 'failed',
      //         duration: 2000,
      //         mask: true,

      //       });
      //       return;
      //     }
      //     //请求成功
      //     var labels = data.labels;
      //     var datas = data.datas;
      //     var itemList = new Array();
      //     for (var index in labels) {
      //       itemList.push(_this.createItem(datas, labels[index]))
      //     }
      //     // console.log("stationName = " + _this.data.name + "\nupdateTime = " + datas[0].update_time)
      //     _this.setData({ stationName: _this.data.name, updateTime: datas[0].update_time, listdata: itemList });

      //   }
      // });
    }
    
  }
  
})
